<?php

namespace App\Services;
use App\Models\Crm_Dynamic;
use App\Models\Crm_Dynamic_Comment;
use App\Models\Crm_Dynamic_Zan;
use App\Traits\External;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class DynamicService
{
    public function updateDynamicStatus($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();
            foreach ($ids as $id) {
                $res = Crm_Dynamic::find($id);
                $res->status = $params['status'];

                if(!$res->save()){
                    throw new \Exception("更新Crm_Dynamic动态状态失败" );
                }
            }

            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function dynamicDel($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();
            foreach ($ids as $id) {
                $res = Crm_Dynamic::find($id);
                $res->is_del = 'Y';

                if(!$res->save()){
                    throw new \Exception("删除Crm_Dynamic动态失败" );
                }
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function updateCommentStatus($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();
            foreach ($ids as $id) {
                $res = Crm_Dynamic_Comment::find($id);
                $res->status = $params['status'];

                if(!$res->save()){
                    throw new \Exception("更新Crm_Dynamic_Comment动态评论状态失败" );
                }
            }

            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function commentDel($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();
            foreach ($ids as $id) {
                $res = Crm_Dynamic_Comment::find($id);
                $res->is_del = 'Y';

                if(!$res->save()){
                    throw new \Exception("删除Crm_Dynamic_Comment动态评论失败" );
                }
            }

            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function getDynamicList($params)
    {
        $result = Crm_Dynamic::getFilter($params)
            ->select(['id','mid','content','status','created_at'])
            ->Nodel()
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        foreach ($result as $key => $value){
            $result[$key]['imgs'] = $value->hasManyDynamicImg()->pluck('img');
            $commit = $value->hasManyDynamicComment()->Nodel()->get();
            $commit = External::optionResult($commit,1);
            $result[$key]['comment'] = $commit;
        }
        $result = External::optionResult($result,1);
        $list['list'] = $result;
        return $list;
    }

    public function getCommentList($params)
    {
        $result = Crm_Dynamic_Comment::getFilter($params)
            ->select(['id','mid','content','dyid','status','created_at'])
            ->Nodel()
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $arr = [];
        foreach ($result as $key => $value){
            $arr[$key]['id'] = $value->id;
            $arr[$key]['mid'] = $value->mid;
            $arr[$key]['content'] = $value->content;
            $arr[$key]['status'] = $value->status;
            $arr[$key]['created_at'] = Carbon::parse($value->created_at)->toDateTimeString();
            $arr[$key]['base']['id'] = $value->hasOneMember->id;
            $arr[$key]['base']['username'] = $value->hasOneMember->username;
            $arr[$key]['base']['realname'] = $value->hasOneMember->realname;
            $arr[$key]['base']['avatar'] = $value->hasOneMember->avatar;
            $arr[$key]['base']['typeText'] = $value->hasOneMember->typeText;
            $arr[$key]['base']['sn'] = $value->hasOneMember->sn;
            $arr[$key]['base']['nationalText'] = $value->hasOneMember->nationalText;
            $arr[$key]['base']['age'] = $value->hasOneMember->age;
            $arr[$key]['base']['height'] = $value->hasOneMember->height;
            $arr[$key]['base']['educationText'] = $value->hasOneMember->educationText;
            $arr[$key]['base']['marryText'] = $value->hasOneMember->marryText;
            $arr[$key]['base']['genderText'] = $value->hasOneMember->genderText;
            $arr[$key]['base']['belongText'] = $value->hasOneMember->belongText;
            $arr[$key]['obase']['id'] = $value->hasOneDynamic->hasOneMember->id;
            $arr[$key]['obase']['username'] = $value->hasOneDynamic->hasOneMember->username;
            $arr[$key]['obase']['realname'] = $value->hasOneDynamic->hasOneMember->realname;
            $arr[$key]['obase']['avatar'] = $value->hasOneDynamic->hasOneMember->avatar;
            $arr[$key]['obase']['typeText'] = $value->hasOneDynamic->hasOneMember->typeText;
            $arr[$key]['obase']['sn'] = $value->hasOneDynamic->hasOneMember->sn;
            $arr[$key]['obase']['nationalText'] = $value->hasOneDynamic->hasOneMember->nationalText;
            $arr[$key]['obase']['age'] = $value->hasOneDynamic->hasOneMember->age;
            $arr[$key]['obase']['height'] = $value->hasOneDynamic->hasOneMember->height;
            $arr[$key]['obase']['educationText'] = $value->hasOneDynamic->hasOneMember->educationText;
            $arr[$key]['obase']['marryText'] = $value->hasOneDynamic->hasOneMember->marryText;
            $arr[$key]['obase']['genderText'] = $value->hasOneDynamic->hasOneMember->genderText;
            $arr[$key]['obase']['belongText'] = $value->hasOneDynamic->hasOneMember->belongText;
        }
        $list['list'] = $arr;
        return $list;
    }


    public function getDynamicZanList($params)
    {
        $result = Crm_Dynamic_Zan::getFilter($params)
            ->select(['id','mid','dyid','is_zan','created_at'])
            ->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $arr = [];
        foreach ($result as $key => $value){
            $arr[$key]['id'] = $value->id;
            $arr[$key]['mid'] = $value->mid;
            $arr[$key]['is_zan'] = $value->is_zan;
            $arr[$key]['created_at'] = Carbon::parse($value->created_at)->toDateTimeString();
            $arr[$key]['imgs'] = $value->hasOneDynamic->hasManyDynamicImg()->pluck('img');
            $arr[$key]['content'] = $value->hasOneDynamic->content;
            $arr[$key]['base']['id'] = $value->hasOneMember->id;
            $arr[$key]['base']['username'] = $value->hasOneMember->username;
            $arr[$key]['base']['realname'] = $value->hasOneMember->realname;
            $arr[$key]['base']['avatar'] = $value->hasOneMember->avatar;
            $arr[$key]['base']['typeText'] = $value->hasOneMember->typeText;
            $arr[$key]['base']['sn'] = $value->hasOneMember->sn;
            $arr[$key]['base']['nationalText'] = $value->hasOneMember->nationalText;
            $arr[$key]['base']['age'] = $value->hasOneMember->age;
            $arr[$key]['base']['height'] = $value->hasOneMember->height;
            $arr[$key]['base']['educationText'] = $value->hasOneMember->educationText;
            $arr[$key]['base']['marryText'] = $value->hasOneMember->marryText;
            $arr[$key]['base']['genderText'] = $value->hasOneMember->genderText;
            $arr[$key]['base']['belongText'] = $value->hasOneMember->belongText;
            $arr[$key]['obase']['id'] = $value->hasOneDynamic->hasOneMember->id;
            $arr[$key]['obase']['username'] = $value->hasOneDynamic->hasOneMember->username;
            $arr[$key]['obase']['realname'] = $value->hasOneDynamic->hasOneMember->realname;
            $arr[$key]['obase']['avatar'] = $value->hasOneDynamic->hasOneMember->avatar;
            $arr[$key]['obase']['typeText'] = $value->hasOneDynamic->hasOneMember->typeText;
            $arr[$key]['obase']['sn'] = $value->hasOneDynamic->hasOneMember->sn;
            $arr[$key]['obase']['nationalText'] = $value->hasOneDynamic->hasOneMember->nationalText;
            $arr[$key]['obase']['age'] = $value->hasOneDynamic->hasOneMember->age;
            $arr[$key]['obase']['height'] = $value->hasOneDynamic->hasOneMember->height;
            $arr[$key]['obase']['educationText'] = $value->hasOneDynamic->hasOneMember->educationText;
            $arr[$key]['obase']['marryText'] = $value->hasOneDynamic->hasOneMember->marryText;
            $arr[$key]['obase']['genderText'] = $value->hasOneDynamic->hasOneMember->genderText;
            $arr[$key]['obase']['belongText'] = $value->hasOneDynamic->hasOneMember->belongText;
        }
        $list['list'] = $arr;
        return $list;
    }

}
